﻿Imports System.Data
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports TepcelDominio
Imports TepcelDAL.DBManager
Public Class ProveedorDAL : Inherits DBManager
    Public Function NuevoProveedor(ByVal proveedor As Proveedor) As String
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("SPInsertProveedor")
        comando.Parameters.Add("@codigoProveedor", SqlDbType.Char, 4).Value = proveedor.codigoProveedor
        comando.Parameters.Add("@nombreProveedor", SqlDbType.VarChar, 80).Value = proveedor.nombreProveedor
        comando.Parameters("@codigoProveedor").Direction = ParameterDirection.InputOutput

        MyBase.ExecuteQuery(comando)
        proveedor.codigoProveedor = comando.Parameters("@codigoProveedor").Value
        Return proveedor.codigoProveedor
    End Function

    Private Function SelectObjectFactory(ByVal comando As SqlCommand) As List(Of Proveedor)
        Dim Lista As New List(Of Proveedor)
        Using reader As SqlDataReader = MyBase.ExecuteReader(comando)
            While reader.Read
                Dim proveedor As New Proveedor(reader.GetString(0), reader.GetString(1))
                Lista.Add(proveedor)
                proveedor = Nothing
            End While
        End Using
        Return Lista
    End Function

    Public Function GetAllFromProveedor() As List(Of Proveedor)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("GetAllFromProveedor")
        Return SelectObjectFactory(comando)
    End Function

    Public Function SearchFromProveedor(ByVal texto As String) As List(Of Proveedor)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("searchProveedor")
        comando.Parameters.Add("@texto", SqlDbType.VarChar, 200).Value = texto
        Return SelectObjectFactory(comando)
    End Function

    Public Function DeleteProveedor(ByVal codigoProveedor As String) As Boolean
        Dim correcto As Boolean = False
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("DeleteProveedor")
        comando.Parameters.Add("@codigoProveedor", SqlDbType.Char, 5).Value = codigoProveedor

        If MyBase.ExecuteQuery(comando) > 0 Then
            correcto = True
        End If
        Return correcto
    End Function

End Class
